package cn.laixueit.controller;

import cn.laixueit.pojo.Users;
import cn.laixueit.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/save")
    public String save(Users users){
        userService.save(users);
        return "redirect:list";
    }

    @PostMapping("/update")
    public String update(Users users){
        userService.update(users);
        return "redirect:list";
    }

    @GetMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page, Model model){
        PageInfo<Users> pageInfo = userService.findAll(page);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    @GetMapping("/findById/{id}")
    public String findById(@PathVariable("id")Integer id, Model model){
        Users users = userService.findById(id);
        model.addAttribute("users",users);
        return "edit";
    }

    @GetMapping("/removeById")
    public String removeById(Integer id){
        userService.removeById(id);
        return "redirect:list";
    }
}
